﻿using ApiData.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Diagnostics;
using System.IO;

namespace ApiData.Web.Controllers
{
    [AllowAnonymous]
    public class HomeController : BaseController
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger, SqlSugarClient context) : base(context)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Verify(Model.user user)
        {
            if (user.account.IsNullOrWhiteSpace()) return Json(Result.Fail("请输入账号"));
            if (user.password.IsNullOrWhiteSpace()) return Json(Result.Fail("请输入密码"));

            var pwd = Common.Security.DESEncrypt(user.password);
            var model = context.Queryable<Model.user>().First(x => x.account == user.account && x.password == pwd);
            if (model == null)
                return Json(Result.Fail("账号或密码错误"));

            LoginUser = model;
            return Json(Result.SuccessData("/App/Index"));
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public IActionResult Doc()
        {
            var path = Directory.GetCurrentDirectory();
            var real = path + "/apidoc.md";
            TextReader reader = new StreamReader(real);
            var text = reader.ReadToEnd();
            Response.Headers.AcceptCharset = "utf-8";
            reader.Close();
            ViewBag.Text = text;
            return View();
        }
    }
}